package promote.brozen.reactor;

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * publishOn  修改的是之后的发布操作 onNext doOnNext onError doOnError doComplete doOnComplete 执行的线程
 *
 * subscribeOn 修改的是之前的订阅操作 onSubscribe doOnSubscribe 以及事件源(publisher)执行的线程
 *
 * 如果subscribeOn修改订阅线程后，没有用publishOn修改发布线程，则后面的发布操作默认和事件源在同一个线程中执行，也在subscribeOn指定的线程
 *
 * @author Brozen
 * @date 2020/8/6 4:28 PM
 * @email brozen@qq.com
 */
public class SubscribeOn {

    public static void main(String[] args) {
        Flux.range(0, 10)
                .map(i -> {
                    System.out.println(Thread.currentThread().getName() + ":" + i + " map");
                    return i * i;
                })
                .doOnSubscribe(s -> {
                    System.out.println(Thread.currentThread().getName() + ": doOnSubscribe 1");
                })
                .doOnRequest(n -> {
                    System.out.println(Thread.currentThread().getName() + ": doOnRequest 1");
                })
                .subscribeOn(Schedulers.newSingle("single1"))
                .doOnNext(i -> {
                    System.out.println(Thread.currentThread().getName() + ":" + i + " doOnNext");
                })
                .doOnSubscribe(s -> {
                    System.out.println(Thread.currentThread().getName() + ": doOnSubscribe 2");
                })
                .doOnRequest(n -> {
                    System.out.println(Thread.currentThread().getName() + ": doOnRequest 2");
                })
                .subscribeOn(Schedulers.newSingle("single2"))
                .subscribe(i -> {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                });
    }

}
